package bll

import (
	log "auth/collector/logger"
	"auth/errors"
	"auth/event"
	"auth/model"
	"auth/store/redis"
	"context"
	"github.com/mojocn/base64Captcha"
)

var captStore = redis.CaptchaDefaultRedisStore()

type captcha struct {
}

var Captcha = &captcha{}

func (c *captcha) init() func() {
	return func() {}
}

func (c *captcha) onEvent(*event.Data) {}

func (c *captcha) Create(ctx context.Context) (*model.CaptchaResponse, error) {
	driver := base64Captcha.NewDriverDigit(80, 240, 80, 0.7, 80)
	cp := base64Captcha.NewCaptcha(driver, captStore.UseWithCtx(ctx)) // v8下使用redis
	id, b64s, err := cp.Generate()
	if err != nil {
		log.Error("验证码获取失败!", err)
		return nil, errors.CaptchaError.Error()
	}
	out := &model.CaptchaResponse{
		CaptchaId:     id,
		PicPath:       b64s,
		CaptchaLength: 80, //global.GVA_CONFIG.Captcha.KeyLong,
	}
	return out, err
}
